package cn.edu.csust.coursecard.rabbitmq;

import cn.edu.csust.coursecard.dao.entity.StuInfo;
import cn.edu.csust.coursecard.dao.StuInfoMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class MQReceiver {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(MQReceiver.class);

    @Autowired
    private StuInfoMapper stuInfoMapper;

    @RabbitListener(queues = "lastLoginTimeQueue")
    public void receive(String message) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            StuInfo stuInfo = mapper.readValue(message, StuInfo.class);
            stuInfo.setLastLoginTime(new Date());
            stuInfoMapper.updateByPrimaryKeySelective(stuInfo);
        } catch (JsonProcessingException e) {
            LOGGER.error("Json解析错误：{}", e.getMessage());
        }
    }
}
